import { mergeOptions , extend } from '../util/index'
import { ASSET_TYPES } from '../../shared/constants'
export function initExtend(Vue) {
    //给vue函数上面挂载extend
    console.log('给vue函数上面挂载extend')
    Vue.cid = 0
    let cid = 1
    Vue.extend = function (extendOptions) {
        extendOptions = extendOptions || {}
        const Super = this;
        const SuperId = Super.cid;
        const cachedCtors = extendOptions._Ctor || (extendOptions._Ctor = {});
        if (cachedCtors[SuperId]) {
            return cachedCtors[SuperId]
        }
        const name = extendOptions.name || Super.options.name;
        const Sub = function VueComponent(options) {
            this._init(options)
        }
        //继承vue原型上的方法
        console.log("继承vue原型上的方法")
        Sub.prototype = Object.create(Super.prototype)
        //保存constructor
        console.log("保存constructor")
        Sub.prototype.constructor = Sub
        Sub.cid = cid++
        //合并到options里components里的template模板
        Sub.options = mergeOptions(
            Super.options,
            extendOptions
        )
        console.log("合并到options里components里的template模板",Sub.options)
        Sub['super'] = Super
        Sub.extend = Super.extend
        Sub.mixin = Super.mixin
        Sub.use = Super.use
        ASSET_TYPES.forEach(function (type) {
            Sub[type] = Super[type]
        })
        Sub.superOptions = Super.options
        Sub.extendOptions = extendOptions
        Sub.sealedOptions = extend({}, Sub.options);
        cachedCtors[SuperId] = Sub
        return Sub
    }
}